package com.ft.adbix.utils.data.gather.common;

import android.content.Context;
import android.os.Build;

import com.ft.adbix.oaid.OAIDManager;
import com.ft.adbix.oaid.util.OAIDUtils;
import com.ft.adbix.utils.data.gather.entity.DataGatherConfig;
import com.ft.adbix.utils.data.gather.manager.DatagatherOaidManager;
import com.ft.adbix.utils.data.gather.util.DataArrangeUtil;
import com.ft.adbix.utils.data.gather.util.device.LocationUtil;
import com.ft.adbix.utils.data.gather.util.device.PhoneStateUtil;
import com.ft.adbix.utils.data.gather.util.device.WifiUtil;
import com.ft.adbix.utils.data.synchronize.OnDeviceDataUpLoadListener;
import com.ft.adbix.utils.data.synchronize.common.DataSynchronizeManager;
import com.ft.adbix.utils.data.synchronize.manager.DataCacheManager;
import com.ft.adbix.utils.data.synchronize.util.InstallListReadTimeController;

/**
 * @author ciba
 * @description 信息收集
 * @date 2018/12/3
 */
public class DataGatherManager {

    private static DataGatherManager instance;
    private boolean checkRoot;
    private Context context;
    private DataGatherConfig config;

    private DataGatherManager() {
    }

    public static DataGatherManager getInstance() {
        if (instance == null) {
            synchronized (DataGatherManager.class) {
                if (instance == null) {
                    instance = new DataGatherManager();
                }
            }
        }
        return instance;
    }

    public void init(Context context, DataGatherConfig config) {
        this.config = config;
        init(context, false);
    }

    /**
     * 初始化
     *
     * @param context   ：上下文
     * @param checkRoot ：是否有必要检查ROOT状态
     */
    public void init(Context context, boolean checkRoot) {
        boot(context, checkRoot);
        initGather();
    }

    /**
     * 初始化
     *
     * @param context   ：上下文
     * @param checkRoot ：是否有必要检查ROOT状态
     */
    public void boot(Context context, boolean checkRoot) {
//        if (Looper.getMainLooper() != Looper.myLooper()) {
//            throw new RuntimeException("Must call in the main thread!!");
//        }

        this.context = context.getApplicationContext();
        this.checkRoot = checkRoot;
        DataSynchronizeManager.getInstance().init(context, DataGatherManager.getInstance().getSdkVersion());
    }

    /**
     * 初始化日志
     */
    public void initGather() {
//        if (context != null && Looper.getMainLooper() == Looper.myLooper() && ProcessUtil.isMainProcess()) {
        if (context != null) {
            if (config != null) {
                WifiUtil.setCanGetWifiInfo(config.isCanGetWifiInfo());
                LocationUtil.setCanUseLocation(config.isCanUseLocation());
                PhoneStateUtil.setCanGetPhoneStateInfo(config.isCanGetPhoneStateInfo());
            }

//            DataArrangeUtil.dataGather(false, true, null, false);
        }
    }

    public boolean isCanUseOaid(){
        if (config != null) {
            return config.isCanUseOaid();
        }
        return true;
    }


    /**
     * 获取machineId给外部调用
     *
     * @param listener
     */
    public void getMachineId(OnDeviceDataUpLoadListener listener) {
        //初始化判断
        getContext();
        long machineId = DataCacheManager.getInstance().getMachineId();
        if (machineId == 0) {
            DataArrangeUtil.dataGather(listener, true);
        } else if (listener != null) {
            listener.onUploadSuccess(machineId);
        }
    }

    /**
     * 获取上下文
     */
    public Context getContext() {
        return context;
    }

    /**
     * 是否有必要获取ROOT信息
     */
    public boolean isCheckRoot() {
        return checkRoot;
    }

    /**
     * 获取并上传手机安装列表信息
     */
    public void upLoadInstallPackage() {
        try {
            final long machineId = DataCacheManager.getInstance().getMachineId();
            if (machineId == 0) {
                return;
            }

            boolean localInstallListRead = InstallListReadTimeController.isLocalInstallListRead();
            if (!localInstallListRead) {
                return;
            }
            InstallListReadTimeController.saveLocalInstallListReadTime();

            //异步获取安装列表并上报，采用读取根目录文件夹的形式判断，移除API方式
            requestInstallPackageList(machineId);
        } catch (Exception e) {
        }
    }

    /**
     * 请求配置扫描目录判断包名
     */
    public void requestInstallPackageList(final long machineId) {
        //判断版本号在29以下
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
            return;
        }

//        Map<String, String> requestParams = new HashMap<>(1);
//        //用dcid 替换 machineId
//        requestParams.put("dcid", machineId + "");
//        String installPackageUrl = SampleUrlManager.getInstallPackageUrl();
//        AsyncHttpClient httpClient = SampleLoaderUploaderManager.getInstance().getHttpClient();
//        httpClient.post(installPackageUrl, requestParams, new HttpListener() {
//                    @Override
//                    public void onRequestSuccess(String result) {
//                        DataSynchronizeLog.innerI("0x00000010");
//                        try {
//                            String resultJson = PckLogJsonUtil.parseDataJson(result);
//                            if (!TextUtils.isEmpty(resultJson)) {
//                                PckLogJsonUtil.packagePathData(machineId, resultJson);
//                            }
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//        );
    }


    public String getSdkVersion() {
        return "1.0";
//        return BuildConfig.VERSION_NAME;
    }


}
